# backend/api/v1/subscription.py - 修复后的完整版本
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from backend.database import get_db
from backend.schemas import subscription as sub_schemas
from backend.crud import subscription as crud
from backend.api.deps import get_current_user  # 添加认证依赖

router = APIRouter(prefix="/subscription", tags=["subscriptions"])

@router.post("/", response_model=sub_schemas.SubscriptionOut)
async def create_subscription(
    data: sub_schemas.SubscriptionCreate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)  # 添加认证
):
    return await crud.create_subscription(db, data)

# 无斜杠版本
@router.get("", response_model=list[sub_schemas.SubscriptionOut])
async def read_subscriptions_no_slash(
    skip: int = 0,
    limit: int = 100,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    return await crud.get_subscriptions(db, skip=skip, limit=limit)

# 有斜杠版本
@router.get("/", response_model=list[sub_schemas.SubscriptionOut])
async def read_subscriptions(
    skip: int = 0,
    limit: int = 100,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    return await crud.get_subscriptions(db, skip=skip, limit=limit)

@router.patch("/{sub_id}", response_model=sub_schemas.SubscriptionOut)
async def update_subscription(
    sub_id: str,
    data: sub_schemas.SubscriptionUpdate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    sub = await crud.update_subscription(db, sub_id, data)
    if not sub:
        raise HTTPException(status_code=404, detail="Subscription not found")
    return sub